package org.file.ftp.transfer;

import com.diogonunes.jcolor.Ansi;
import com.diogonunes.jcolor.Attribute;
import lombok.extern.slf4j.Slf4j;
import org.file.ftp.transfer.service.FtpTransfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class FtpTransferApplication implements CommandLineRunner
{
    public static final  String banner=""" 
                                   
                                   :: File Transfer Common Client over FTP             [2025-08-16] ::
                                          ________________     _________            __
                                         / ____/_  __/ __ \\   / ____/ (_)__  ____  / /_
                                        / /_    / / / /_/ /  / /   / / / _ \\/ __ \\/ __/
                                       / __/   / / / ____/  / /___/ / /  __/ / / / /_
                                      /_/     /_/ /_/       \\____/_/_/\\___/_/ /_/\\__/
        """;

    public static void print_banner(){
        System.out.println(Ansi.colorize(banner, Attribute.BRIGHT_GREEN_TEXT()));
        System.out.println(Ansi.colorize("                            :: FTP Client for Auto Transfer ::", Attribute.BRIGHT_BLUE_TEXT(), Attribute.BOLD()) +
                Ansi.colorize("     (v1.2.0)  ", Attribute.BRIGHT_CYAN_TEXT()) +
                Ansi.colorize("powered by KXingFang\n", Attribute.BRIGHT_YELLOW_TEXT())
        );
    }

    @Autowired
    FtpTransfer ftpTransfer;
    public static void main(String[] args) {
        System.setProperty("file.encoding", "UTF-8");
        System.setProperty("console.encoding", "GBK");
        SpringApplication.run(FtpTransferApplication.class);
    }

    @Override
    public void run(String... args) throws Exception {
        print_banner();
        while(true) {
            try {
                try {
                    String clearScreen = "\033[H\033[2J";
                    System.out.print(clearScreen);
                    System.out.flush(); // 确保输出被立即发送
                } catch (Exception e) {
                    System.err.println("Clear screen failed: " + e.getMessage());
                }

                ftpTransfer.start();
                print_banner();
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                ftpTransfer.close();
                log.info(Ansi.colorize("㊣----------------------✎ ☯ sleeping ☯ ✐----------------------㊣",
                        Attribute.BRIGHT_YELLOW_TEXT()));
                Thread.sleep(5000);
            }
        }
    }
}